layui.use(['layer', 'form', 'admin', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var upload=layui.upload;

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxd/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');

    form.on('select(dm)', function(data){
        var eqy=data.value;
        if(eqy==""){
            $("#dm").html("");
            $("#dm").append("<option value=''>全部</option>");
            form.render('select');
        }else{
            var qyAjax = new $ax(Feng.ctxPath + "/btyBxd/getBxr");
            qyAjax.set("dm",data.value);
            var qyResult = qyAjax.start();
            $("#btyBxdaId").html("");
            $("#btyBxdaId").append("<option value=''>全部</option>");
            $.each(qyResult, function( index, val ) {
                $("#btyBxdaId").append("<option value='"+val.id+"'>"+val.xm+"</option>");
            });
            form.render('select');
        }
    });

    /**
     * 检索报销人
     */
    // var languageAjax = new $ax(Feng.ctxPath + "/btyBxd/getBxr");
    // var languageResult = languageAjax.start();
    // $.each(languageResult, function( index, val ) {
    //     $("#btyBxdaId").append("<option value='"+val.id+"'>"+val.xm+"</option>");
    // } );
    // form.render('select');
    //普通图片上传
    var uploadInst = upload.render({
        elem: '#upload_btn',
        url: Feng.ctxPath + '/btyBxd/upload',
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $("#name").val(file.name);
                //拼接附件样式
                $("#layer-photos-demo").html(
                    "<div class='gs-upload-file-container'>"+
                    "<div class='gs-upload-ele'>"+
                    "<div class='progress-bar progress-bar-success progress-bar-striped gs-upload-progress-bar' style='width: 188px; display: none;'></div>" +
                    "<div class='gs-upload-ele-img-div'>"+
                    "<img class='gs-upload-ele-img' src='' style='cursor: pointer;width: 150px;height: 150px;' id='gs-upload-ele-img'>	"+
                    "</div>	"+
                    "<div class='gs-upload-ele-content'>"+
                    "<div class='gs-upload-ele-title-div'></div>"+
                    "<div class='gs-upload-ele-menu'>"+
                    "</div></div></div></div>"
                );

                //回填图片src
                $("#gs-upload-ele-img").attr("src",result);
                //图片预览
                layer.photos({
                    photos: '#layer-photos-demo',
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            });
        },
        done: function(res){
            //如果上传失败
            if(res.code === 1){
                return layer.msg("上传失败");
            }
            //上传成功
            $("#picture").val(res.data.src);
            layer.msg("上传成功");
        },
        error: function(){
            //演示失败状态，并实现重传
            var msgText = $('#msgText');
            msgText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
            msgText.find('.upload-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/btyBxd/add", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        // $(function() {
        //     $( "#noticeForm" ).sisyphus({
        //         onRelease: function() {
        //             alert("数据已经清除！");
        //         }//清除先前保存的数据触发这个函数
        //     });
        // });

        //添加 return false 可成功跳转页面
        return false;

    });
});